Decomposing scRNA-seq data using NMF - a demo
Non-negative matrix factorization is a tool for the analysis of high dimensional data that allows extracting sparse and meaningful features from a set of non-negative data vectors. It is well suited for decomposing scRNA-seq data, effectively reducing large complex matrices (\(10^4\) of genes times \(10^5\) of cells) into a few interpretable gene programs. It has been especially used to extract recurrent gene programs in cancer cells (see e.g. Barkely et al. (2022) and Gavish et al. (2023)), which are otherwise difficult to integrated and analyse jointly.
Here, to illustrate the methods implemented in the GeneNMF package, we will apply NMF on a single-cell cell dataset of human PBMCs - a downsampled version of the dataset published by Hao et al. (2021).
Set up the enviroment
Here are some packages you’ll need for this demo:
library(Seurat)
library(ggplot2)
library(UCell)
library(patchwork)
library(Matrix)
library(RcppML)
library(dendextend)
library(viridis)
remotes::install_github("carmonalab/GeneNMF", ref="dev")
library(GeneNMF)Then download the test dataset for this demo.
ddir <- "input"
data.path <- sprintf("%s/pbmc_multimodal.downsampled20k.seurat.rds", ddir)
if (!file.exists(data.path)) {
dir.create(ddir)
dataUrl <- "https://www.dropbox.com/s/akzu3hp4uz2mpkv/pbmc_multimodal.downsampled20k.seurat.rds?dl=1"
download.file(dataUrl, data.path)
}
seu <- readRDS(data.path)NMF for dimensionality reduction
NMF can be applied to reduce the dimensionality of the data from tens
of thousand of genes to a few dimensions (similarly to PCA). With the
RunNMF() function, it can be directly applied on a Seurat
object, and it will save the NMF results as a new dimensionality
reduction.
ndim <- 15
seu <- FindVariableFeatures(seu, nfeatures = 1000)
seu <- RunNMF(seu, k = ndim, assay="SCT")## A dimensional reduction object with key NMF_
## Number of dimensions: 15
## Number of cells: 20000
## Projected dimensional reduction calculated: FALSE
## Jackstraw run: FALSE
## Computed using assay: SCT
We can also further reduced the dimensionality to 2 dimensions using UMAP; in this space we can visualize all cells in a single plot.
seu <- RunUMAP(seu, reduction = "NMF", dims=1:ndim, reduction.name = "NMF_UMAP", reduction.key = "nmfUMAP_")DimPlot(seu, reduction = "NMF_UMAP", group.by = "celltype.l1", label=T) + theme(aspect.ratio = 1,
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank()) + ggtitle("NMF UMAP") + NoLegend()Consistent NMF programs across multiple samples
Identification of robust gene programs requires their detection
across samples and variability of input parameters. Perhaps the most
crucial parameter to NMF is the dimensionality k, which
corresponds to the number of programs of the low-dimensional matrix. To
determine robust programs, we can run NMF over multiple numbers of
k and determine programs that are consistenly found across
these runs. The multiNMF() function automatically performs
NMF over a list of samples and for multiple values of
k:
seu.list <- SplitObject(seu, split.by = "donor")
geneNMF.programs <- multiNMF(seu.list, assay="SCT", slot="data", k=4:9, L1=c(0,0),
do_centering=TRUE, calculate_hvg = TRUE, nfeatures = 2000,
exclude_ribo_mito=FALSE)We can now combine the gene programs identified over multiple samples
and numbers of k into metaprograms (MPs),
i.e. consensus programs that are robustly identified across NMF runs.
Here we will define 10 MPs:
geneNMF.metaprograms <- getMetaPrograms(geneNMF.programs, max.genes=50,
hclust.method="ward.D2", nprograms=10, min.confidence=0.3)It can be useful to visualize pairwise similarity (Jaccard Index)
between individual gene programs that compose meta-programs. We can see
“blocks” corresponding to gene programs of high similarity across
datasets and values of k. We can then cut the similarity
tree at a given height to find blocks of similar programs and derive
consensus gene signatures for each block. For example, here we cut the
tree to the height corresponding to 10 clusters of programs (the
MPs):
We can also inspect useful statistics, such as the “sample coverage” (in what fraction of samples the MP was detected); or the silhoette coefficient (how similar are individual programs in a MP relative to programs in other MPs - the higher the better).
## sampleCoverage silhouette meanJaccard numberGenes
## MetaProgram1 1.000 0.42727458 0.526 50
## MetaProgram2 1.000 0.54641836 0.566 50
## MetaProgram3 1.000 0.49076316 0.542 50
## MetaProgram4 1.000 0.57626146 0.599 50
## MetaProgram5 1.000 0.46332602 0.506 50
## MetaProgram6 1.000 0.34023361 0.381 47
## MetaProgram7 1.000 0.21014105 0.347 50
## MetaProgram8 0.875 0.31852560 0.345 47
## MetaProgram9 0.875 0.32154887 0.377 50
## MetaProgram10 1.000 0.03431189 0.172 42
What are the genes driving each program?
## [,1] [,2] [,3] [,4] [,5] [,6]
## MetaProgram1 "CD14" "CXCL8" "CYP1B1" "EGR1" "FCN1" "FOS"
## MetaProgram2 "BANK1" "CD79A" "FCRL5" "IGHD" "IGHM" "LINC00926"
## MetaProgram3 "ACRBP" "CAVIN2" "CLEC1B" "PRKAR2B" "ITGA2B" "SPARC"
## MetaProgram4 "ADGRG1" "CTSW" "FGFBP2" "GNLY" "GZMB" "IL2RB"
## MetaProgram5 "CCR7" "LEF1" "LRRN3" "MAL" "PASK" "TSHZ2"
## MetaProgram6 "C1QA" "CDKN1C" "CKB" "CTSL" "HES4" "LYPD2"
## MetaProgram7 "SERPINB2" "VCAN" "PLBD1" "RBP7" "S100A12" "S100A9"
## MetaProgram8 "CLEC10A" "ENHO" "FCER1A" "PID1" "C1orf54" "CD1C"
## MetaProgram9 "CAMK2N1" "GZMK" "KLRG1" "LINC01871" "LYAR" "MAF"
## MetaProgram10 "IER3" "C15orf48" "CCL3L1" "SGK1" "CXCL8" "G0S2"
Intepretation of gene programs by GSEA
To aid the interpretation of gene programs, we can compare them to
known signatures from public databases. The runGSEA()
function can be useful to scan msigDB and evaluate the overlap of
detected gene programs with signatures in the databases. Here we compare
to the “C8” category (cell type signature gene sets); but other classes
such as “H” (hallmark gene sets) may be more relevant in other
contexts.
top_p <- lapply(geneNMF.metaprograms$metaprograms.genes, function(program) {
runGSEA(program, universe=rownames(seu), category = "C8")
})For example, Program 2 appears to correlate significantly with a B cell signature:
## pathway pval
## <char> <num>
## 1: DURANTE_ADULT_OLFACTORY_NEUROEPITHELIUM_B_CELLS 1.911106e-34
## 2: AIZARANI_LIVER_C34_MHC_II_POS_B_CELLS 7.037844e-34
## 3: FAN_EMBRYONIC_CTX_BRAIN_B_CELL 2.314615e-30
## 4: DURANTE_ADULT_OLFACTORY_NEUROEPITHELIUM_PLASMA_CELLS 3.413731e-30
## 5: TRAVAGLINI_LUNG_B_CELL 1.968309e-26
## 6: RUBENSTEIN_SKELETAL_MUSCLE_B_CELLS 1.966404e-23
## padj overlap size overlapGenes
## <num> <int> <int> <list>
## 1: 1.333952e-31 16 34 BANK1, C....
## 2: 2.456207e-31 21 131 BANK1, B....
## 3: 5.385339e-28 18 95 BANK1, C....
## 4: 5.956961e-28 13 21 CD79A, D....
## 5: 2.747760e-24 18 152 BANK1, B....
## 6: 2.287584e-21 17 175 BANK1, C....
Signature scores for gene programs
A simple way to evaluate gene programs learned from the data is to
calculate gene signature scores with the UCell package.
mp.genes <- geneNMF.metaprograms$metaprograms.genes
seu <- AddModuleScore_UCell(seu, features = mp.genes, assay="SCT", ncores=4, name = "")We can see how many of the programs are enriched in specific cell subtypes (cell type annotation from the original study).
Signature scores to define integrated space
Individual cells can now be represented in terms of their gene program scores. Importantly, here the gene programs were learned as a consensus of gene programs found across multiple samples – as opposed to calculating NMF once on the whole dataset. This can be an effective strategy to mitigate batch effects, as meta-programs (MPs) are a consensus of gene programs consistently found across individual samples. Let’s store these coordinates in the Seurat object:
matrix <- seu@meta.data[,names(mp.genes)]
dimred <- scale(matrix)
colnames(dimred) <- paste0("MP_",seq(1, ncol(dimred)))
#New dim reduction
seu@reductions[["MPsignatures"]] <- new("DimReduc",
cell.embeddings = dimred,
assay.used = "RNA",
key = "MP_",
global = FALSE)We can also use these scores to generate a UMAP representation and visualize the data in 2D:
set.seed(123)
seu <- RunUMAP(seu, reduction="MPsignatures", dims=1:length(seu@reductions[["MPsignatures"]]),
metric = "euclidean", reduction.name = "umap_MP")How do the signature scores for the meta-programs look like in the combined space?
FeaturePlot(seu, features = names(mp.genes), reduction = "umap_MP", ncol=4) &
scale_color_viridis(option="B") &
theme(aspect.ratio = 1, axis.text=element_blank(), axis.ticks=element_blank())Final remarks
NMF can be a powerful tool to extract gene programs for scRNA-seq data in an unbiased manner. Because it is calculated for each sample separately, it bypasses the need to perform batch effect correction to analyse samples jointly. This aspect makes it particularly interesting for the analysis of gene programs in cancer cells (see e.g. Barkely et al. (2022) and Gavish et al. (2023).